package com.yourenbang.lingyun.transaction.util.lakala;

import com.yourenbang.lingyun.constants.BizCodeMessages;
import com.yourenbang.lingyun.core.exception.BizRuntimeException;
import com.yourenbang.lingyun.transaction.util.lakala.replace.SM2Utils;
import com.yourenbang.lingyun.transaction.util.lakala.replace.Util;
import com.yourenbang.transaction.api.service.pay.PayTypeApiService;
import org.bouncycastle.util.encoders.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Component
public class LakalaReplaceEnUtil {

    private static final Logger logger = LoggerFactory.getLogger(LakalaReplaceEnUtil.class);

    private static PayTypeApiService payTypeApiService;

    @Autowired
    public void setLakalaReplaceConfig(PayTypeApiService payTypeApiService) {
        LakalaReplaceEnUtil.payTypeApiService = payTypeApiService;
    }

    /**
     * 功能: SM2，加密
     *
     * @param plainText
     * @return
     * @throws Exception
     * @date 2017年12月8日
     * @author tongxs
     */
//    public static String encrypt(String plainText) throws Exception {
//        Map<String, String> typeParam = payTypeApiService.getPayParams(PayTypeEnum.UNIONPAY, PayFromEnum.withdraw_ally);
//        String tpublicKey = new String(Base64.encode(Util.hexToByte(typeParam.get("publicKey"))));
//        byte[] tCipherText = SM2Utils.encrypt(Base64.decode(tpublicKey.getBytes()), plainText.getBytes());
//        String cipherText = new String(Base64.encode(tCipherText));
//        return cipherText;
//    }

    /**
     * 功能: SM2，解密
     *
     * @param cipherText
     * @return
     * @throws Exception
     * @date 2017年12月8日
     * @author tongxs
     */
//    public static String decrypt(String cipherText) throws Exception {
//        Map<String, String> typeParam = payTypeApiService.getPayParams(PayTypeEnum.UNIONPAY, PayFromEnum.withdraw_ally);
//        String tprivateKey = new String(Base64.encode(Util.hexToByte(typeParam.get("privateKey"))));
//        byte[] tcipherText = Base64.decode(cipherText);
//        byte[] tPlainText = SM2Utils.decrypt(Base64.decode(tprivateKey.getBytes()), tcipherText);
//        // String plainText = new String(Base64.decode(tPlainText));
//        String plainText = new String(tPlainText);
//        return plainText;
//
//    }

//    /**
//     * 功能: 使用国密算法签名报文
//     *
//     * @param userId
//     *            国密规范用户ID
//     * @param message
//     * @return
//     * @date 2017年12月8日
//     * @author tongxs
//     */
//    public static String sign(String userId, String message) {
//        logger.info("【拉卡拉代付计算签名】入参:userId:{}, message:{}", userId, message);
//        Map<String, String> typeParam = new HashMap<>(); //payTypeApiService.getPayParams(PayTypeEnum.UNIONPAY, PayFromEnum.withdraw_ally);
//        String tprivateKey = new String(Base64.encode(Util.hexToByte(typeParam.get("privateKey"))));
//        byte[] signdata = null;
//        try {
//            signdata = SM2Utils.sign(userId.getBytes(), Base64.decode(tprivateKey.getBytes()), message.getBytes());
//        } catch (IOException e) {
//            logger.error("【拉卡拉代付计算签名】失败,{},入参:userId:{}, message:{}", e.getMessage(), userId, message, e);
//            throw new BizRuntimeException(BizCodeMessages.UNKNOWN_ERROR, "计算签名失败");
//        }
//        String signValue = Util.getHexString(signdata);
//        logger.info("【拉卡拉代付计算签名】计算结果:{}, 入参:userId:{}, message:{}", signValue, userId, message);
//        return signValue;
//    }

    /**
     * 功能: 使用国密算法验签报文
     *
     */
//    public static boolean verifySign(String userId, String message, String signData) {
//        logger.info("【拉卡拉代付验签】入参:userId:{}, message:{}, signData:{}", userId, message, signData);
//        Map<String, String> typeParam = payTypeApiService.getPayParams(PayTypeEnum.UNIONPAY, PayFromEnum.withdraw_ally);
//        String tpublicKey = new String(Base64.encode(Util.hexToByte(typeParam.get("publicKey"))));
//        byte[] tSignData = Util.hexToByte(signData);
//        boolean result = false;
//        try {
//            result = SM2Utils.verifySign(userId.getBytes(), Base64.decode(tpublicKey.getBytes()),
//                    message.getBytes(), tSignData);
//        } catch (IOException e) {
//            logger.error("【拉卡拉代付验签】失败,{}, 入参:userId:{}, message:{}, signData:{}", e.getMessage(), userId, message, signData, e);
//            throw new BizRuntimeException(BizCodeMessages.UNKNOWN_ERROR, "验签报文失败");
//        }
//        logger.info("【拉卡拉代付验签】结果:{}, 入参:userId:{}, message:{}, signData:{}", result, userId, message, signData);
//        return result;
//    }

}
